//index.js
import store from '../../store/index'
import { login } from '../../api/login'
import { getSchools, getTags } from '../../api/code'
import { hasKey, error, isUndefined, success } from '../../lib/js/utils'
import { updateUser, getUserInfo } from '../../api/user'

// 创建页面
Page(
  store.createPage({
    /**
     * 提交表单
     */
    doSubmit: function(e) {
      const that = this
      const tagIds = []
      that.data.tagPicker.forEach((item, index) => {
        if (item.isSelected) {
          tagIds.push(item.id)
        }
      })

      const formData = {
        gender: that.data.gender,
        imageId: that.data.imageId,
        nickname: e.detail.value.nickname,
        schoolId: that.data.schoolId,
        signature: e.detail.value.signature,
        tagIds: tagIds
      }
      // 修改用户信息
      updateUser(formData).then(function(res) {
        success('修改成功，请回到个人中心下拉刷新数据')
      })
    },
    /**
     * 初始化数据
     */
    initData: function() {
      const that = this
      that.setData({
        gender: that.data.userInfo.gender,
        imageId: that.data.userInfo.avatar.id,
        nickname: that.data.userInfo.nickname,
        schoolId: that.data.userInfo.school.id,
        signature: that.data.userInfo.signature,
        tagIds: that.data.userInfo.tags.map(tag => {
          return tag.id
        })
      })
      getTags().then(function(res) {
        res.data.forEach((item, index) => {
          if (that.data.tagIds.indexOf(item.id) !== -1) {
            // 存在改标签
            res.data[index].isSelected = true
          } else {
            res.data[index].isSelected = false
          }
        })
        that.setData({
          tagPicker: res.data
        })
      })
      getSchools().then(function(res) {
        that.setData({
          schoolPicker: res.data
        })
      })
    },
    /**
     * 选择标签
     * @param {*} params
     */
    selecteTag: function(e) {
      const that = this
      const tagIndex = e.currentTarget.dataset.index
      var tagPicker = that.data.tagPicker
      var flag = tagPicker[tagIndex].isSelected
      tagPicker[tagIndex].isSelected = !flag
      that.setData({
        tagPicker: tagPicker
      })
    },
    /**
     * 显示风格标签选择面板
     * @param {*} e
     */
    showTagsVisible: function(e) {
      this.setData({
        showTagsVisible: true
      })
    },
    /**
     * 显示风格标签选择面板， 进行关闭
     * @param {*} e
     */
    closeShowTagsVisible: function(e) {
      this.setData({
        showTagsVisible: false
      })
    },
    /**
     * 改变性别
     * @param {} e
     */
    changeGender: function(e) {
      const that = this
      const index = e.detail.value
      that.setData({
        gender: that.data.genderPicker[index].id
      })
    },
    /**
     * 改变学校
     * @param {} e
     */
    changeSchool: function(e) {
      const that = this
      const index = e.detail.value
      that.setData({
        schoolId: that.data.schoolPicker[index].id
      })
    },
    data: {
      // 提交表单
      gender: null,
      imageId: null,
      nickname: null,
      schoolId: null,
      signature: null,
      tagIds: null,

      // 可见
      showTagsVisible: false,
      // 选择器
      genderPicker: [
        {
          id: 0,
          name: '女'
        },

        {
          id: 1,
          name: '男'
        }
      ],
      schoolPicker: [],
      tagPicker: []
    },
    globalData: ['userInfo', 'token'],

    onLoad() {
      this.initData()
    }
  })
)
